package com.videoaggre.zcl.helper.player.cover

import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.kk.taurus.playerbase.config.PConst
import com.kk.taurus.playerbase.event.BundlePool
import com.kk.taurus.playerbase.event.EventKey
import com.kk.taurus.playerbase.event.OnPlayerEventListener
import com.kk.taurus.playerbase.receiver.BaseCover
import com.kk.taurus.playerbase.utils.NetworkUtils
import com.videoaggre.zcl.R
import com.videoaggre.zcl.common.bindViewClick
import com.videoaggre.zcl.helper.player.DataInter
import com.videoaggre.zcl.ui.widget.CommonButton

/**
 * Created by Taurus on 2018/4/20.
 */
class ErrorCover(context: Context?) : BaseCover(context) {
    val STATUS_ERROR = -1
    val STATUS_UNDEFINE = 0
    val STATUS_MOBILE = 1
    val STATUS_NETWORK_ERROR = 2
    var mStatus = STATUS_UNDEFINE
    private val mInfo: TextView by lazy { view.findViewById(R.id.tvErrorInfo) }
    private val mRetry: CommonButton by lazy { view.findViewById(R.id.tvRetry) }
    private var mErrorShow = false
    private var mCurrPosition = 0
    override fun onCoverAttachedToWindow() {
        super.onCoverAttachedToWindow()
        handleStatusUI(NetworkUtils.getNetworkState(context))
    }

    override fun onReceiverBind() {
        bindViewClick(::onViewClick, mRetry)
    }

    private fun onViewClick(view: View) {
        when (view) {
            mRetry -> handleStatus()
        }
    }

    private fun handleStatus() {
        val bundle = BundlePool.obtain()
        bundle.putInt(EventKey.INT_DATA, mCurrPosition)
        when (mStatus) {
            STATUS_ERROR -> {
                setErrorState(false)
                requestRetry(bundle)
            }
            STATUS_MOBILE -> {
                setErrorState(false)
                requestResume(bundle)
            }
            STATUS_NETWORK_ERROR -> {
                setErrorState(false)
                requestRetry(bundle)
            }
        }
    }

    override fun onProducerData(key: String, data: Any) {
        super.onProducerData(key, data)
        if (DataInter.Key.KEY_NETWORK_STATE == key) {
            val networkState = data as Int
            if (networkState == PConst.NETWORK_STATE_WIFI && mErrorShow) {
                val bundle = BundlePool.obtain()
                bundle.putInt(EventKey.INT_DATA, mCurrPosition)
                requestRetry(bundle)
            }
            handleStatusUI(networkState)
        }
    }

    private fun handleStatusUI(networkState: Int) {
        if (!groupValue.getBoolean(DataInter.Key.KEY_NETWORK_RESOURCE, true)) return
        if (networkState < 0) {
            mStatus = STATUS_NETWORK_ERROR
            setErrorInfo("无网络！")
            setHandleInfo("重试")
            setErrorState(true)
        } else {
            if (networkState == PConst.NETWORK_STATE_WIFI) {
                if (mErrorShow) {
                    setErrorState(false)
                }
            } else {
//                if (App.ignoreMobile) return
//                mStatus = STATUS_MOBILE
//                setErrorInfo("您正在使用移动网络！")
//                setHandleInfo("继续")
//                setErrorState(true)
            }
        }
    }

    private fun setErrorInfo(text: String) {
        mInfo.text = text
    }

    private fun setHandleInfo(text: String) {
        mRetry.text = text
    }

    private fun setErrorState(state: Boolean) {
        mErrorShow = state
        setCoverVisibility(if (state) View.VISIBLE else View.GONE)
        if (!state) {
            mStatus = STATUS_UNDEFINE
        } else {
            notifyReceiverEvent(DataInter.Event.EVENT_CODE_ERROR_SHOW, null)
        }
        groupValue.putBoolean(DataInter.Key.KEY_ERROR_SHOW, state)
    }

    override fun onPlayerEvent(eventCode: Int, bundle: Bundle?) {
        when (eventCode) {
            OnPlayerEventListener.PLAYER_EVENT_ON_DATA_SOURCE_SET -> {
                mCurrPosition = 0
                handleStatusUI(NetworkUtils.getNetworkState(context))
            }
            OnPlayerEventListener.PLAYER_EVENT_ON_TIMER_UPDATE -> mCurrPosition = bundle?.getInt(EventKey.INT_ARG1) ?: 0
        }
    }

    override fun onErrorEvent(eventCode: Int, bundle: Bundle?) {
        mStatus = STATUS_ERROR
        if (!mErrorShow) {
            setErrorInfo("出错了！")
            setHandleInfo("重试")
            setErrorState(true)
        }
    }

    override fun onReceiverEvent(eventCode: Int, bundle: Bundle?) {}
    public override fun onCreateCoverView(context: Context): View {
        return View.inflate(context, R.layout.view_cover_error, null)
    }

    override fun getCoverLevel(): Int {
        return levelHigh(0)
    }
}